/***********************************************
 **** 版权声明处 **
 ****  为了方便阅读和维护，请严格遵守相关代码规范，谢谢   ****
 *******************************************/
package com.hnisi.board.view
{
	/*******************************************
	 **** huangjixin,2012-6-12,上午9:50:07 **
	 **** 仪表板面板  **
	 *******************************************/
	import com.hnisi.board.common.Global;
	import com.hnisi.board.controller.DashBoardEvent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.core.DragSource;
	import mx.core.FlexGlobals;
	import mx.core.IVisualElementContainer;
	import mx.core.UIComponent;
	import mx.effects.Parallel;
	import mx.events.DragEvent;
	import mx.events.SandboxMouseEvent;
	import mx.graphics.ImageSnapshot;
	import mx.managers.DragManager;
	
	import spark.components.Application;
	import spark.components.Button;
	import spark.components.Group;
	import spark.components.Image;
	import spark.components.Panel;
	import spark.components.ToggleButton;
	import spark.effects.Move;
	import spark.effects.Resize;
	
	[Event(name="max", type="com.hnisi.board.controller.DashBoardEvent")]
	
	public class DashBoardPanel extends Panel
	{
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 类私有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：private static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 类公有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：public static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 私有变量声明处，请以“_”开头定义变量
		// 例如：private var _example:String;
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		private var _parentContainer:IVisualElementContainer;
		private var _index:int;
		private var _width:Number;
		private var _height:Number;
		
		private var _percWidth:Number;
		private var _percHeight:Number;
		
		private var _draggable:Boolean;
		
		private var point:Point = new Point();
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 公有变量声明处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		[SkinPart]
		public var dragImage:Image;
		
		[SkinPart]
		public var firGroup:Group;
		
		[SkinPart]
		public var collspeBtn:ToggleButton;
		
		[SkinPart]
		public var maxBtn:ToggleButton;
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 构造函数，初始化相关工作可以放在里面
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		public function DashBoardPanel()
		{
			super();
			draggable = true;
			
//			addEventListener(DragEvent.DRAG_OVER,onDragOver);
			addEventListener(DragEvent.DRAG_ENTER,onDragEnter);
			addEventListener(DragEvent.DRAG_DROP,onDragDrop);
			addEventListener(DragEvent.DRAG_EXIT,onDragExit);
		}
		
		//构造函数结束
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// getter和setter函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		/**
		 * 是否允许拖拽 
		 * @return 
		 * 
		 */
		public function get draggable():Boolean
		{
			return _draggable;
		}
		
		public function set draggable(value:Boolean):void
		{
			_draggable = value;
		}
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 相关事件响应函数和逻辑函数存放处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

		

		/**
		 * 拖拽开始； 
		 * @param event
		 * 
		 */
		protected function onDragImageMouseDown(event:MouseEvent):void
		{
			if(!draggable){
				return;
			}
			
			var dragInitiator:DashBoardPanel=this;
			var dragSource:DragSource = new DragSource();
			var image:Image = new Image();
			image.source = ImageSnapshot.captureBitmapData(this,null,null,null,null,true);
			dragSource.addData({},Global.getInstance().dataFormat);
			DragManager.doDrag(dragInitiator,dragSource,event,image,0,0,1);
			
			this.visible = false;
			systemManager.getSandboxRoot().addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
		}
		
		protected function onMouseUp(event:Event):void
		{
			this.visible = true;
		}		
		
		
		protected function onDragDrop(event:DragEvent):void
		{
			clearRect();
			var dragIntiator:DashBoardPanel = event.dragInitiator as DashBoardPanel;
			if(dragIntiator == this){
				return;
			}
			if(this.parent.contains(dragIntiator)){
				if(this.parent is IVisualElementContainer){
					IVisualElementContainer(this.parent).swapElements(dragIntiator,this);
				}
			}else{
				var index:int = IVisualElementContainer(this.parent).getElementIndex(this);
				if(index != -1){
					IVisualElementContainer(this.parent).addElementAt(dragIntiator,index);
				}
			}
			
		}
		
		protected function onDragEnter(event:DragEvent):void
		{
			if(event.dragSource.hasFormat(Global.getInstance().dataFormat)){
				/*if(event.dragInitiator == this){
					return;
				}*/
				DragManager.acceptDragDrop(DashBoardPanel(event.currentTarget));
				
				drawRect();
			}
		}
		
		protected function onDragExit(event:DragEvent):void
		{
			clearRect();
		}
		
		protected function onDragOver(event:DragEvent):void
		{
			if(event.dragSource.hasFormat(Global.getInstance().dataFormat)){
				drawRect();
			}
		}
		
		protected function drawRect():void{
			firGroup.graphics.clear();
			firGroup.graphics.lineStyle(4,0x3A3A82);
			firGroup.graphics.drawRect(firGroup.x,firGroup.y,firGroup.width,firGroup.height);
		}
		
		protected function clearRect():void{
			firGroup.graphics.clear();
		}
		
		protected function onCollspeBtnClick(event:MouseEvent):void
		{
//			collspeBtn.to
			this.height = 0;
		}
		
		
		/**
		 * 最大化； 
		 * @param event
		 * 
		 */
		protected function onMaxBtnClick(event:MouseEvent):void
		{
			if(maxBtn.selected){
				_parentContainer = this.parent as IVisualElementContainer;
				_index = this._parentContainer.getElementIndex(this);
				_width = this.width;
				_height = this.height;
				_percWidth = this.percentWidth;
				_percHeight = this.percentHeight;
				
				/*var point:Point = new Point(this.x,this.y);
				point = this.localToGlobal(point);
				this.x = point.x;
				this.y = point.y;
				var move:Move = new Move(this);
				move.xFrom = point.x;
				move.yFrom = point.y;
				move.xTo = Global.getInstance().dashBoardContainerRect.x;
				move.yTo = Global.getInstance().dashBoardContainerRect.y;
				
				var resize:Resize = new Resize(this);
				resize.widthFrom = this.width;
				resize.heightFrom = this.height;
				resize.widthTo = Global.getInstance().dashBoardContainerRect.width;
				resize.heightTo = Global.getInstance().dashBoardContainerRect.height;
				var parall:Parallel = new Parallel();
				parall.duration = 2000;
				parall.addChild(move);
				parall.addChild(resize);
				parall.play();*/
				
				point = new Point(Global.getInstance().dashBoardContainer.x,Global.getInstance().dashBoardContainer.y);
				point = Global.getInstance().dashBoardContainer.parent.localToGlobal(point);
				point = Application(FlexGlobals.topLevelApplication).globalToLocal(point);
				FlexGlobals.topLevelApplication.addElement(this);
				/*this.x= point.x;
				this.y= point.y;
				this.width = Global.getInstance().dashBoardContainerRect.width;
				this.height = Global.getInstance().dashBoardContainerRect.height;*/
				this.x= 0;
				this.y= 0;
				this.width = FlexGlobals.topLevelApplication.width;
				this.height = FlexGlobals.topLevelApplication.height;
				
				this.draggable = false;
				
				this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
				
				var dashBoardEvent:DashBoardEvent = new DashBoardEvent(DashBoardEvent.MAX,this,true);
				this.dispatchEvent(dashBoardEvent);
			}else{
				this.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
				
				_parentContainer.addElementAt(this,_index);
				this.width = _width;
				this.height = _height;
				this.percentWidth = _percWidth;
				this.percentHeight = _percHeight;
				this.draggable = true;
			}
			
		}
		
		protected function onEnterFrame(event:Event):void
		{
			point.x = Global.getInstance().dashBoardContainer.x;
			point.y = Global.getInstance().dashBoardContainer.y;
			point = Global.getInstance().dashBoardContainer.parent.localToGlobal(point);
			point = Application(FlexGlobals.topLevelApplication).globalToLocal(point);

			/*this.x= point.x;
			this.y= point.y;
			this.width = Global.getInstance().dashBoardContainerRect.width;
			this.height = Global.getInstance().dashBoardContainerRect.height;*/
			this.x= 0;
			this.y= 0;
			this.width = FlexGlobals.topLevelApplication.width;
			this.height = FlexGlobals.topLevelApplication.height;
		}
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// override 覆盖函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		override protected function partAdded(partName:String, instance:Object):void{
			super.partAdded(partName,instance);
			if(instance == dragImage){
				dragImage.addEventListener(MouseEvent.MOUSE_DOWN, onDragImageMouseDown);
			}
			if(instance == collspeBtn){
				collspeBtn.addEventListener(MouseEvent.CLICK, onCollspeBtnClick);
			}
			if(instance == maxBtn){
				maxBtn.addEventListener(MouseEvent.CLICK, onMaxBtnClick);
			}
		}
		
		override protected function partRemoved(partName:String, instance:Object):void{
			super.partRemoved(partName,instance);
			if(instance == dragImage){
				dragImage.removeEventListener(MouseEvent.MOUSE_DOWN, onDragImageMouseDown);
			}
			if(instance == collspeBtn){
				collspeBtn.removeEventListener(MouseEvent.CLICK, onCollspeBtnClick);
			}
		}
		
	}//类结束
}//包结束